﻿#if !defined(_AMACHINE_H_)
#define _AMACHINE_H_

#include "AState.h"
#include "AValue.h"

class CAXMLNode;

class CAMachine : public ICAState
{
protected:
	CAString _file;
	CAState* _root;
	CAStates* _all;
	CAValues* _conditionResults;
public:
	CAMachine();
	virtual ~CAMachine();

	void addStateIntoAllStatesTable(CAState* ps);
	void removeStateFromAllStatesTable(CAState* ps);
	CAState* findState(const CAString& fullname);
    CAStates* getAllAStates();

    void initialize(CAXMLNode* pnode);
	void navigateToState(ccTime time, CAState* from, const CAString& tostate);

	virtual int init() { return 0; };
	virtual int deinit() { return 0; };
	virtual int Update(ccTime time);
    virtual int Enter(ccTime time);
	virtual int Leave(ccTime time);

	virtual CAState* getCurrentState();
	inline CAState* getLeafState() { return getCurrentState()->getLeafState(); }

	void setConditionResult(const char* key, bool bResult);
	//bool getConditionResult(const char* key);

    virtual bool checkCondition(CAState* from, const CATransition& trans);
	virtual void OnEventNotify(CAState* from, EStateEvent se, void* param = null);
	virtual void onStateBegin(CAState* from, void* param) {};
	virtual void onStateUpdate(CAState* from, size_t counter, ccTime time) {};
	virtual void onStateEnd(CAState* from, void* param) {};

    //virtual int DoAction(const CAState& from, const Action& act) = 0;
};

#endif /*_AMACHINE_H_*/